package  
{
	import assets.RondaFont;

	import com.kvc.extend.papervision3d.Book3D;

	import org.papervision3d.view.BasicView;

	import flash.display.GradientType;
	import flash.display.StageQuality;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;

	/**
	 * @author Kevin Cao
	 */
	[SWF(width="1000", height="600", backgroundColor="0x222222", frameRate="30")]
	public class Book3DDemo extends BasicView 
	{

		private var book : Book3D;
		private var xml : XML;

		private var instruction : TextField;

		public function Book3DDemo() 
		{
			stage.quality = StageQuality.LOW;
			
			RondaFont;
			
			var loader : URLLoader = new URLLoader();
			var request : URLRequest = new URLRequest("xml/book.xml");
			loader.addEventListener(Event.COMPLETE, completeHandler);
			loader.load(request);
		}

		private function completeHandler(event : Event) : void 
		{
			xml = new XML(event.target.data);
			init();
		}

		private function init() : void 
		{
			book = new Book3D(xml);
			book.rotationX = 15;
			scene.addChild(book);
			
			camera.focus = 180;
			camera.zoom = 3;
			camera.z = -1000;
			
			viewport.containerSprite.filters = [new DropShadowFilter(15, 90, 0, 1, 30, 30, 0.5)];

			startRendering();
			
			instruction = new TextField();
			instruction.defaultTextFormat = new TextFormat("ronda", 8, 0xcccccc);
			instruction.embedFonts = true;
			instruction.autoSize = TextFieldAutoSize.LEFT;
			instruction.text = "Press Up and Down Key on your keyboard.";
			addChild(instruction);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			stage.addEventListener(Event.RESIZE, resizeHandler);
			
			resizeHandler();
		}

		private function keyDownHandler(event : KeyboardEvent) : void
		{
			switch (event.keyCode) 
			{
				case Keyboard.DOWN:
				case Keyboard.PAGE_DOWN:
					book.pageDown();
					break;
				case Keyboard.UP:				case Keyboard.PAGE_UP:
					book.pageUp();
					break;
			}
		}

		private function resizeHandler(event : Event = null) : void
		{
			instruction.x = (stage.stageWidth - instruction.width) / 2;
			instruction.y = stage.stageHeight - instruction.height - 5;
			drawBackground();
		}

		private function drawBackground() : void 
		{
			var matrix : Matrix = new Matrix();
			matrix.createGradientBox(stage.stageWidth, stage.stageHeight);
			
			graphics.clear();
			graphics.beginGradientFill(GradientType.RADIAL, [0x777777, 0x222222], [1, 1], [0, 255], matrix);
			graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			graphics.endFill();
		}

		protected override function onRenderTick(event : Event = null) : void 
		{
			camera.x += ((stage.mouseX - stage.stageWidth / 2) - camera.x) * 0.15;			camera.y += ((stage.mouseY - stage.stageHeight / 2) - camera.y) * 0.15;
			super.onRenderTick();
		}
	}
}
